package com.example.simple_calculator;

import javafx.fxml.FXML;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class CalculatorController {

    @FXML
    private TextField display;

    private SimpleCalculator calculator;
    private boolean startNumber = true;
    private double number1;
    private String operator = "";
    @FXML
    private  Button btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9;
    @FXML
    private  Button btn_0,btn_m,btn_d,btn_a,btn_s,btn_p,btn_e,btn_i;


    @FXML
    private void initialize() {
        calculator = new SimpleCalculator();
    }

    @FXML
    private void onKeyTyped(KeyEvent event)
    {
        System.out.println(event.getCode());
        System.out.println(event.getCharacter());
        String keyPressed = event.getCharacter();
        char c = keyPressed.charAt(0);

        switch(c) {
            case '1': {
                btn_1.requestFocus();
                btn_1.fire();
                return;
            }
            case '2': {
                btn_2.requestFocus();
                btn_2.fire();
                return;
            }
            case '3': {
                btn_3.requestFocus();
                btn_3.fire();
                return;
            }
            case '4': {
                btn_4.requestFocus();
                btn_4.fire();
                return;
            }
            case '5': {
                btn_5.requestFocus();
                btn_5.fire();
                return;
            }
            case '6': {
                btn_6.requestFocus();
                btn_6.fire();
                return;
            }
            case '7': {
                btn_7.requestFocus();
                btn_7.fire();
                return;
            }
            case '8': {
                btn_8.requestFocus();
                btn_8.fire();
                return;
            }
            case '9': {
                btn_9.requestFocus();
                btn_9.fire();
                return;
            }
            case '0': {
                btn_0.requestFocus();
                btn_0.fire();
                return;
            }
            case '+': {
                btn_p.requestFocus();
                btn_p.fire();
                return;
            }
            case '-': {
                btn_s.requestFocus();
                btn_s.fire();
                return;
            }
            case '*': {
                btn_m.requestFocus();
                btn_m.fire();
                return;
            }
            case '/': {
                btn_d.requestFocus();
                btn_d.fire();
                return;
            }
            case '=': {
                btn_e.requestFocus();
                btn_e.fire();
                return;
            }
            case 0x8://backspace
            case 0x2e://delete
            {
                btn_i.requestFocus();
                btn_i.fire();
                return;
            }
        }
    }

    @FXML
    private void processDigit(ActionEvent event) {
        String digitPressed = ((Button) event.getSource()).getText();
        System.out.println(digitPressed);
        if(digitPressed.equals("DEL")){
           if(display.getText().length()>1){
                display.setText(display.getText().substring(0,display.getText().length()-1));
            }
            else if(!display.getText().equals("0")){
               display.setText("0");
            }
            return;
        }
        if (startNumber || display.getText().equals("0")) {
            display.setText(digitPressed);
        } else {
            display.setText(display.getText() + digitPressed);
        }
        startNumber = false;
    }

    @FXML
    private void processOperator(ActionEvent event) {
        String operatorPressed = ((Button) event.getSource()).getText();
        System.out.println(operatorPressed);
        if (operatorPressed.equals("=")) {
            if (operator.isEmpty()) {
                return;
            }
            double number2 = Double.parseDouble(display.getText());
            double result = calculator.calculate(number1, number2, operator);
            display.setText(String.format("%.0f", result));
            operator = "";
        }
        else {
            if (! operator.isEmpty()) {
                return;
            }
            number1 = Double.parseDouble(display.getText());
            operator = operatorPressed;
        }
        startNumber =  true;
    }

}